/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyCondition;

import MyDataContainer.*;
import MyDataType.*;
import java.util.ArrayList;
/**
 *
 * @author Welcome
 */
public class MyCondition {

    //<editor-fold defaultstate="collapsed" desc="1/ Các thuộc tính cơ bản">
    private MyQuery myQuery;
    private String conditionString;
    private String leftSide;
    private String rightSide;
    private String signString;
    private String[] listSign={"=",">","<",">=","<=","like","in"};
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="2/ Các phương thức khởi tạo">
    public MyCondition()
    {
        this.conditionString="";
        this.leftSide="";
        this.rightSide="";
        this.myQuery = new MyQuery();
    }
    public MyCondition(MyQuery query,String condition) {
        this.conditionString=condition;
        this.leftSide="";
        this.rightSide="";
        this.myQuery = new MyQuery(query);
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="3/ Các phương thức Get/Set">
    public String getConditionString() {
        return conditionString;
    }
    
    public void setConditionString(String conditionString) {
        this.conditionString = conditionString;
    }

    public String getLeftSide() {
        return leftSide;
    }

    public String getRightSide() {
        return rightSide;
    }

    public String getSignString() {
        return signString;
    }

    public MyQuery getMyQuery() {
        return myQuery;
    }
    
    
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="4/ Cac phuong thuc xu ly nghiep vu">
    public ConditionType identifyCondition()
    {
        ConditionType kind=ConditionType.ATT_ATT;
        signString = findSignString(conditionString);
        
        if(!signString.equals(""))
        {
            String[] temp = conditionString.split(signString);
            leftSide = temp[0].trim();
            rightSide = temp[1].trim();
        }
        
        if(rightSide.indexOf("(")>-1)
            kind=ConditionType.TUP_QUE;
        else
        {
            int left=-1;
            for(int i=0; i<myQuery.getColumnTitles().getSize();i++)
            {
                left  = myQuery.getColumnTitles().getAt(i).toLowerCase().indexOf(leftSide);
                if(left >-1)
                    break;
            }
            
            int right=-1;
            for(int i=0; i<myQuery.getColumnTitles().getSize();i++)
            {
                right  = myQuery.getColumnTitles().getAt(i).toLowerCase().indexOf(rightSide);
                if(right >-1)
                    break;
            }
            
            if(left>-1 && right>-1)
                kind=ConditionType.ATT_ATT;
            else
                kind=ConditionType.ATT_PAT;
        }
        
        return kind;
    }
    public String findSignString(String condExpress)
    {
        
        String result="";
        for(int i=0; i<listSign.length;i++)
        {
            int temp = condExpress.indexOf(listSign[i]);
            if(temp>-1)
            {
                result = listSign[i];
                return result;
            }
        }
        
        return result;
    }
    
    
    
    public void processCondition() {
        
        ConditionType kind = identifyCondition();
        
        switch(kind)
        {
            case ATT_ATT:
                //Điều kiện lọc lại data, thành iner join
                processForATT_ATT();
                break;
            case ATT_PAT:
                //Điều kiện so sánh data với 1 pattern
                processForATT_PAT();
                break;
            case TUP_QUE:
                //so sánh data với 1 tập hợp
                processForTUP_QUE(leftSide,signString,rightSide);
                break;
        }
        
    }

    private void processForATT_ATT() {
        MyQuery resultQuery = new MyQuery(myQuery);
        resultQuery.clearAll();
        
        int column1 = myQuery.getColumnTitles().getPosition(leftSide);
        int column2 = myQuery.getColumnTitles().getPosition(rightSide);
        
        for(int i=0; i<myQuery.getSize();i++)
        {
            MyDataSet rowDataSet = myQuery.getRowAt(i);
            MyData leftData = rowDataSet.getDataAt(column1);
            MyData rightData = rowDataSet.getDataAt(column2);
            boolean temp = leftData.comparisonWith(rightData, signString);
            if(temp==true)
                resultQuery.addContent(rowDataSet);
        }
        
        myQuery=resultQuery;
    }

    private void processForATT_PAT() {
        MyQuery resultQuery = new MyQuery(myQuery);
        resultQuery.clearAll();
        
        int columnPos = myQuery.getColumnTitles().getPosition(leftSide);
        for(int i=0; i<myQuery.getSize();i++)
        {
            MyDataSet rowDataSet = myQuery.getRowAt(i);
            MyData leftData = rowDataSet.getDataAt(columnPos);
            MyData rightData=null;
            MyType type = leftData.getType();
            //Mai lam tip tu day
            switch(type)
            {
                case STRING:
                    rightData = new MyString(rightSide);
                    break;
                case INT:
                    rightData = new MyInt(rightSide);
                    break;
                case DATE:
                    break;
            }
            
            boolean temp = leftData.comparisonWith(rightData, signString);
            if(temp)
                resultQuery.addContent(rowDataSet);
        }
        
        myQuery=resultQuery;
    }

    private void processForTUP_QUE(String leftSide, String signString, String rightSide) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
    
    private ArrayList<Integer> attAtt() {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private ArrayList<Integer> attPat() {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private ArrayList<Integer> tupQue() {
        throw new UnsupportedOperationException("Not yet implemented");
    }
    
    //</editor-fold>


}
